<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\store\StoreMsg;
use Biz\model\store\StoreBasic;
use Doraemon\model\Im\RabbitMessage;
use Doraemon\model\Im\RabbitMessageRelation;
use Doraemon\tools\Log;
use Doraemon\tools\Sms;

//获取门店uid
const START_MSG_TIME = "09:00";
const END_MSG_TIME = "22:00";

if (time() > strtotime(START_MSG_TIME) && time() <= strtotime(END_MSG_TIME)) {
    $haveSendUids = getSendUids();
    $store_uids = getAllStoreUids($haveSendUids);
    [$uids, $fromuids] = getSendtMsgUid($store_uids);
    if($uids && $fromuids) {
        sendMsg($uids, $fromuids);
    }
}

//获取已经发送短信的用户
function getSendUids()
{
    $day = date("Y-m-d");
    $uids = StoreMsg::getUidsByIMKey($day) ?: [];
    return $uids;
}


function getAllStoreUids($haveSendUids)
{
    $where = [];
    //$where['is_test'] = 1;
    if ($haveSendUids && is_array($haveSendUids)) {
        $where['uid[!]'] = $haveSendUids;
    }
    $uids = StoreBasic::getStoreByFilter($where, 'uid');
    return $uids;
}


function getSendtMsgUid($storeUids)
{
    $uids = [];
    $from_uids = [];
    $info = RabbitMessage::select(
        [
            "[>]" . RabbitMessageRelation::_TABLE_NAME => [
                "from_uid" => "from_uid",
                'to_uid' => "to_uid",
                'msgkey' => 'msgkey'
            ],
        ],
        [
            RabbitMessage::_TABLE_NAME . ".id",
            RabbitMessage::_TABLE_NAME . ".to_uid",
            RabbitMessage::_TABLE_NAME . ".from_uid",
            RabbitMessage::_TABLE_NAME . ".add_time",
            RabbitMessageRelation::_TABLE_NAME . ".reply_time",
            RabbitMessageRelation::_TABLE_NAME . ".msgkey",
            RabbitMessageRelation::_TABLE_NAME . ".first_message_time"
        ],
        [
            RabbitMessage::_TABLE_NAME . ".to_uid" => $storeUids,
            RabbitMessage::_TABLE_NAME . ".add_time[>=]" => strtotime(START_MSG_TIME),
            RabbitMessage::_TABLE_NAME . ".add_time[<]" => strtotime(END_MSG_TIME),
            'GROUP' => [RabbitMessage::_TABLE_NAME . ".to_uid", RabbitMessage::_TABLE_NAME . ".from_uid"]
        ]
    );
    if ($info && is_array($info)) {

        $day = date("Y-m-d");

        foreach ($info as $val) {
            //超二小时未回复的用户的首条消息
            if ($val['reply_time'] <= 0 && time() - $val['add_time'] >= 3600 * 2) {

                if(StoreMsg::getSentUidByImFromUid($day,$val['to_uid'],$val['from_uid'])){
                    continue;
                }

                $uids[] = $val['to_uid'];
                $from_uids[$val['to_uid']][] = $val['from_uid'];
                continue;
            }
            if ($val['reply_time'] > 0 && $val['add_time'] - $val['reply_time'] >= 3600 * 2) {

                if(StoreMsg::getSentUidByImFromUid($day,$val['to_uid'],$val['from_uid'])){
                    continue;
                }

                $uids[] = $val['to_uid'];
                $from_uids[$val['to_uid']][] = $val['from_uid'];
                continue;
            }
        }
    }
    return [array_unique($uids), $from_uids];
}

function sendMsg($uids, $from_uids)
{
    $stroePhone = StoreBasic::getStoreByFilter(['uid' => $uids], ['uid', 'phone']);
    if ($stroePhone && is_array($stroePhone)) {
        $aAliyunTemp = ['aliyun' => ['template' => Sms::TYPE_IM_REMIND_STORE]];

        $day = date("Y-m-d");

        foreach ($stroePhone as $val) {

            $result = Sms::sendTplMsg($val['phone'], $aAliyunTemp);
            if (!$result) {
                Log::error(date("Y-m-d H:i:s") . '-发送短信失败 门店uid' . ':' . $val['uid'] . "phone :" . $val['phone']);
                continue;
            }
            //记录发送的消息日志
            StoreMsg::addUidByIMKey($day, $val['uid']);

            foreach ($from_uids[$val['uid']] as $from_uid) {
                StoreMsg::addSentUidByImFromUid($day, $val['uid'], $from_uid);
            }

            Log::info(date("Y-m-d H:i:s") . '-发送短信 门店uid' . ':' . $val['uid'] . "phone :" . $val['phone']);
        }
    }

}





